﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================
// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy
//===============================================================================

using System.Diagnostics;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using EntLibContrib.Logging.TraceListeners;

namespace EntLibContrib.Logging.Configuration
{
	/// <summary>
	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
	/// Represents the process to build a <see cref="NoGuidRollingFlatFileTraceListener"/> described by a <see cref="NoGuidRollingFlatFileTraceListenerData"/> configuration object.
	/// </summary>
	/// <remarks>This type is linked to the <see cref="NoGuidRollingFlatFileTraceListenerData"/> type and it is used by the  Custom Factory
	/// to build the specific <see cref="TraceListener"/> object represented by the configuration object.
	/// </remarks>
	public class NoGuidRollingTraceListenerAssembler : TraceListenerAsssembler
	{
		#region Public Methods
		/// <summary>
		/// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
		/// Builds a <see cref="NoGuidRollingFlatFileTraceListener"/> based on an instance of <see cref="FlatFileTraceListenerData"/>.
		/// </summary>
		/// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
		/// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="FlatFileTraceListenerData"/>.</param>
		/// <param name="configurationSource">The source for configuration objects.</param>
		/// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
		/// <returns>
		/// A fully initialized instance of <see cref="NoGuidRollingFlatFileTraceListener"/>.
		/// </returns>
		/// <seealso cref="TraceListenerCustomFactory"/>
		public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
		{
			NoGuidRollingFlatFileTraceListenerData castObjectConfiguration
					= (NoGuidRollingFlatFileTraceListenerData)objectConfiguration;

			ILogFormatter formatter = GetFormatter(context, castObjectConfiguration.Formatter, configurationSource, reflectionCache);

			NoGuidRollingFlatFileTraceListener createdObject = new NoGuidRollingFlatFileTraceListener(
				castObjectConfiguration.FileName,
				castObjectConfiguration.Header,
				castObjectConfiguration.Footer,
				formatter,
				castObjectConfiguration.MaxInstances,
				castObjectConfiguration.ThrowMax,
				castObjectConfiguration.RollSizeKB,
				castObjectConfiguration.TimeStampPattern,
				castObjectConfiguration.RollFileExistsBehavior,
				castObjectConfiguration.RollInterval,
				castObjectConfiguration.RevolvingDays
				);

			return createdObject;
		}
		#endregion
	}
}
